var SqlClient = require('../lib/mysql/mysql');
var accountDao = module.exports;
var common = require('../common/common');


/**
 * 获取账号信息
 */
accountDao.getAccountInfo = function (userId,cb) {
    var fields=[
        'income_today',
        'income_total',
        'balance_total',
        'state',
        'head_img_url',
        'rb_users.user_id'

    ];
    var sqlSelect = 'select ' + fields.join(',');
    var sqlFrom = ' from rb_account ';
    var sqlJoin =  'left join rb_users on rb_users.user_id = rb_account.user_id '
    var sqlCondition = 'where rb_account.user_id = ' + userId;
    var sql=sqlSelect+sqlFrom+sqlJoin+sqlCondition;
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {

        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }

        console.log(data);
        return cb && cb(null, data);
    })

};

/*
* 创建账号
* */
accountDao.createAccount = function (userId,cb) {
    var sql = 'insert into rb_account (user_id) values(' + userId + ')';
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {

        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }

        console.log(data);
        return cb && cb(null, data);
    })
};

/*
* 修改账户余额
* 较少时money为负数
*
* */
accountDao.updateAccountBalance = function (userId,money,cb) {
    var sql = 'update rb_account set balance_total = balance_total +' + money + ' where user_id =' + userId ;
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {

        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }

        console.log(data);
        return cb && cb(null, data);
    })
}

//更新账号收入
accountDao.updateAccountIncome = function(userId,money,cb){
    var sql = 'update rb_account set income_total = income_total +' + money + ',income_today = income_today +' + money + ' where user_id =' + userId ;
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {
        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}

//增加账号流水
accountDao.addAccountBill = function(orderNo,money,userId,cb){
    var accountBillNo = common.uuid();
    var fields = [
        'account_bill_no',
        'order_no',
        'money',
        'user_id'
    ];

    var values = [
        accountBillNo,
        orderNo,
        money,
        userId
    ];

    var sql = 'insert into rb_account_bills (' + fields.join(',') + ') values ("' + values.join('","') + '")';
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {
        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }

        console.log(data);
        return cb && cb(null, data);
    })



}


/*
*
* 把指定用户的balance_to_cash 清零
*
* */

accountDao.updateBalanceToCash = function(userId,cb){
    var sql = '';
    var sqlUpdate = 'update rb_account ';
    var sqlSet = 'set balance_to_cash = 0 ';
    var sqlCondition = ' where user_id = ' + userId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    SqlClient.query(sql,null,function(err, data) {
        if (err) {
            console.log(err);
            return cb && cb(err, null);
        }

        console.log(data);
        return cb && cb(null, data);
    })


}